<template>
  <div class="program">
    <c-title :title="title"></c-title>
    <ul class="com">
      <li v-for="item in infoList" :key="item.id">
        <a >
          <p-item
            :id="item.id"
            :albumId="item.albumId"
            :anchorId="item.anchorId"
            :title="item.title"
            :isPlay="item.isPlay"
            :playPath="item.playPath"
            @play-pause="play"
          >
         
          </p-item>
        </a>
      </li>
    </ul>
    <div class="add_load">
      <p>
        <a @click="addLoadMore"
          >{{ infoList.length >= this.countItem ? "没有更多内容" : "加载更多"
          }}<van-icon name="arrow-down"
        /></a>
      </p>
    </div>
  </div>
</template>

<script>
import { initAlbumListData } from "@/request/api.js";
import PItem from "@/views/album/albumcomp/PItem.vue";
import { Toast } from 'vant';

export default {
  data() {
    return {
      title: "节目",
      page: 1,
      pageSize: 10,
      infoList: [],
      countItem: 0,
      delay: null,
      
    };
  },
  props: {
    id: {
      type: Number,
    },
  },
  components: {
    PItem,
  },
  created() {
    console.log(this.id);
    this.initListData();
  },
  methods: {
    play(obj){
        
        this.infoList.forEach(item=>{
          if(item.id == obj.id){
            if(item.playPath){
              item.isPlay = obj.isPlay
              this.$emit('play-pause',{playPath:item.playPath,isPlay:obj.isPlay})
            }else{
              Toast.fail("需要购买会员")
              return
            }

            
          }else{
            item.isPlay = false
          }
        })
    },
    initListData() {
      let data = {
        albumId: this.id,
        page: this.page,
        pageSize: this.pageSize,
        asc: true,
      };
      initAlbumListData(data).then((res) => {
        let { data } = res.data;
        this.countItem = data.totalCount;
        console.log("666",res.data)
        this.title = `${this.title}(${data.totalCount})`;
        data.trackDetailInfos.forEach((item) => {
          let obj = {
            // 具体的听书id
            id: item.id,
            albumId: item.trackInfo.albumId,
            anchorId: item.trackInfo.anchorId,
            title: item.trackInfo.title,
            playPath:item.trackInfo.playPath,

            isPlay:false,
          };

          this.infoList.push(obj);
        });
      });
    },

    addLoadMore() {
      if (this.delay) {
        clearInterval(this.delay);
      }

      this.delay = setTimeout(() => {
        if (this.infoList.length >= this.countItem) {
          console.log("没有更多内容了");
          return;
        }

        this.page++;
        let list = [];
        let data = {
          albumId: this.id,
          page: this.page,
          pageSize: this.pageSize,
          asc: true,
        };
        initAlbumListData(data).then((res) => {
          let { data } = res.data;

          data.trackDetailInfos.forEach((item) => {
            let obj = {
              // 具体的听书id
              id: item.id,
              albumId: item.trackInfo.albumId,
              anchorId: item.trackInfo.anchorId,
              title: item.trackInfo.title,
              playPath:item.trackInfo.playPath,
              isPlay:false,
            };

            list.push(obj);
          });

          this.infoList = [...this.infoList, ...list];
        });
      }, 300);

      this.delay = null;
    },
  },
};
</script>

<style lang="scss" scoped>
.program {
  padding: 15px 15px 0;
  .com {
    li {
      border-bottom: 1px solid #f3f4f5;
      a {
        display: block;
      }
    }
  }
  .add_load {
    p {
      text-align: center;
      a {
        line-height: 1.4;
        font-size: 15px;
        color: #f86442;
        font-weight: 700;
        i {
          margin-left: 5px;
        }
      }

      padding: 17px 0;
    }
  }
}
</style>

